﻿using MovingChameLibrary.EnumTypes;
using MovingChameLibrary.Models;
using MovingChameLibrary.ServiceManager;
using MovingChameLibrary.TourServiceReference;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MovingChameLibrary.ViewModels
{
    public abstract class AdvancedSearchViewModel : BaseViewModel
    {
        #region Bindable Properties
        private string _keyword;
        public string Keyword
        {
            get { return this._keyword; }
            set { SetProperty(ref this._keyword, value, "Keyword"); }
        }

        private TourType _tourType;
        public TourType TourType
        {
            get { return this._tourType; }
            set { SetProperty(ref this._tourType, value, "TourType"); }
        }

        private int _maxBudget;
        public int MaxBudget
        {
            get { return this._maxBudget; }
            set { SetProperty(ref this._maxBudget, value, "MaxBudget"); }
        }

        private int _tourLength;
        public int TourLength
        {
            get { return this._tourLength; }
            set { SetProperty(ref this._tourLength, value, "TourLength"); }
        }

        private CustomObservableCollection<Tour> _items;
        public CustomObservableCollection<Tour> Items
        {
            get
            {
                if (this._items == null)
                    this._items = new CustomObservableCollection<Tour>();
                return this._items;
            }
            set { this._items = value; }
        }
        #endregion

        #region Search Properties
        private SortDirection _sortingDirection;
        public SortDirection SortingDirection
        {
            get { return this._sortingDirection; }
            set { this._sortingDirection = value; }
        }

        private SortCriteria _sortingCriteria;
        public SortCriteria SortingCriteria
        {
            get { return this._sortingCriteria; }
            set { this._sortingCriteria = value; }
        }
        #endregion

        #region Command
        protected RelayCommand _sortByTitleCommand;
        public abstract ICommand SortByTitleCommand { get; }

        protected RelayCommand _sortByBudgetCommand;
        public abstract ICommand SortByBudgetCommand { get; }

        protected RelayCommand _searchCommand;
        public abstract ICommand SearchCommand { get; }
        #endregion

        #region Sort Functions
        protected void resetSort()
        {
            _sortingCriteria = SortCriteria.Title;
            _sortingDirection = SortDirection.Ascending;
        }

        protected void sortByTitleAscending()
        {
            _items.Sort(o => o.Title, SortDirection.Ascending);
            _sortingCriteria = SortCriteria.Title;
            _sortingDirection = SortDirection.Ascending;
        }

        protected void sortByTitleDescending()
        {
            _items.Sort(o => o.Title, SortDirection.Descending);
            _sortingCriteria = SortCriteria.Title;
            _sortingDirection = SortDirection.Descending;
        }

        protected void sortByBudgetAscending()
        {
            _items.Sort(o => o.Budget, SortDirection.Ascending);
            _sortingCriteria = SortCriteria.Budget;
            _sortingDirection = SortDirection.Ascending;
        }

        protected void sortByBudgetDescending()
        {
            _items.Sort(o => o.Budget, SortDirection.Descending);
            _sortingCriteria = SortCriteria.Budget;
            _sortingDirection = SortDirection.Descending;
        }
        #endregion

        #region Search Functions
        protected void setSearchData()
        {
            ServiceFactory.GetTourServiceClient().SearchTourAdvancedAsync(Keyword, MaxBudget, TourType, TourLength);
            ServiceFactory.GetTourServiceClient().SearchTourAdvancedCompleted += AdvancedSearchViewModel_SearchTourAdvancedCompleted;
        }

        private void AdvancedSearchViewModel_SearchTourAdvancedCompleted(object sender, SearchTourAdvancedCompletedEventArgs e)
        {
            Items = new CustomObservableCollection<Tour>();
            foreach (TourData tourData in e.Result)
            {
                Items.Add(DataConverter.ConvertToTour(tourData, null));
            }
        }
        #endregion
    }
}